<?php

/**
 * This is the model class for table "page".
 *
 * The followings are the available columns in table 'page':
 * @property string $id
 * @property string $name_en
 * @property string $name_th
 * @property string $short_description_en
 * @property string $short_description_th
 * @property string $description_en
 * @property string $description_th
 * @property string $created_date
 * @property string $updated_date
 * @property string $slug
 * @property integer $deleteable
 */
class Pages extends CActiveRecord
{
  /**
   * Returns the static model of the specified AR class.
   * @param string $className active record class name.
   * @return Page the static model class
   */
  public static function model($className=__CLASS__)
  {
    return parent::model($className);
  }

  /**
   * @return string the associated database table name
   */
  public function tableName()
  {
    return 'pages';
  }

  /**
   * @return array validation rules for model attributes.
   */
  public function rules()
  {
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    return array(
      array('name_en', 'required'),
      array('deleteable', 'numerical', 'integerOnly'=>true),
      array('name_en, name_th, slug', 'length', 'max'=>100),
      array('short_description_en, short_description_th, description_en, description_th, created_date, updated_date', 'safe'),
      // The following rule is used by search().
      // Please remove those attributes that should not be searched.
      array('id, name_en, name_th, short_description_en, short_description_th, description_en, description_th, created_date, updated_date, slug, deleteable', 'safe', 'on'=>'search'),
    );
  }

  /**
   * @return array relational rules.
   */
  public function relations()
  {
    // NOTE: you may need to adjust the relation name and the related
    // class name for the relations automatically generated below.
    return array(
    );
  }

  /**
   * @return array customized attribute labels (name=>label)
   */
  public function attributeLabels()
  {
    return array(
      'id' => 'ID',
      'name_en' => 'Name En',
      'name_th' => 'Name Th',
      'short_description_en' => 'Short Description En',
      'short_description_th' => 'Short Description Th',
      'description_en' => 'Description En',
      'description_th' => 'Description Th',
      'created_date' => 'Created Date',
      'updated_date' => 'Updated Date',
      'slug' => 'Slug',
      'deleteable' => 'Deleteable',
    );
  }

  /**
   * Retrieves a list of models based on the current search/filter conditions.
   * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
   */
  public function search()
  {
    // Warning: Please modify the following code to remove attributes that
    // should not be searched.

    $criteria=new CDbCriteria;

    $criteria->compare('id',$this->id,true);
    $criteria->compare('name_en',$this->name_en,true);
    $criteria->compare('name_th',$this->name_th,true);
    $criteria->compare('short_description_en',$this->short_description_en,true);
    $criteria->compare('short_description_th',$this->short_description_th,true);
    $criteria->compare('description_en',$this->description_en,true);
    $criteria->compare('description_th',$this->description_th,true);
    $criteria->compare('created_date',$this->created_date,true);
    $criteria->compare('updated_date',$this->updated_date,true);
    $criteria->compare('slug',$this->slug,true);
    $criteria->compare('deleteable',$this->deleteable);

    return new CActiveDataProvider($this, array(
      'criteria'=>$criteria,
    ));
  }

  public function behaviors(){
    return array(
      'sluggable' => array(
        'class'=>'ext.SluggableBehavior.SluggableBehavior',
        'columns' => array('name_en'),
        'unique' => true,
        'update' => true,
      ),
    );
  }

  public function beforeSave() {
    if ($this->isNewRecord) {
      $this->created_date = new CDbExpression('NOW()');
    }else {
      $this->updated_date = new CDbExpression('NOW()');
    }
    return parent::beforeSave();
  }

  //virtual field

  public function getName()
  {
    $ret = $this->name_en;

    if (isset($_GET['language'])) {
      switch ($_GET['language']) {
        case "th":
          $data = $this->name_th;
          $ret = (isset($data) && ($data!=''))  ? $data : $ret;
          break;
        default:
          $ret = $this->name_en;
      }
    }

    return $ret;
  }

  public function getShortDescription()
  {
    $ret = $this->short_description_en;

    if (isset($_GET['language'])) {
      switch ($_GET['language']) {
        case "th":
          $data = $this->short_description_th;
          $ret = (isset($data) && ($data!=''))  ? $data : $ret;
          break;
        default:
          $ret = $this->short_description_en;
      }
    }

    return $ret;
  }

  public function getDescription()
  {
    $ret = $this->description_en;

    if (isset($_GET['language'])) {
      switch ($_GET['language']) {
        case "th":
          $data = $this->description_th;
          $ret = (isset($data) && ($data!=''))  ? $data : $ret;
          break;
        default:
          $ret = $this->description_en;
      }
    }

    return $ret;
  }

}